import Vue from "vue";
import VueRouter from "vue-router";
import findLast from 'lodash/findLast'
import {notification} from 'ant-design-vue';
import NotFound from "./views/404.vue";
import Forbidden from "./views/403.vue";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {check, isLogin} from './utils/auth'

Vue.use(VueRouter);

const routes = [
    {
        path: '/user',
        hideInMenu: true,
        component: () =>
            import("./layouts/UserLayout.vue"),
        children: [
            {
                path: '/user/login',
                name: 'login',
                component: () =>
                    import("./views/User/Login.vue")
            },
            {
                path: '/user/register',
                name: 'register',
                component: () =>
                    import("./views/User/Register.vue"),
            },
            {
                path: '/user',
                redirect: '/user/login'
            }
        ]
    },
    {
        path: "/",
        meta: { authority: ["user","admin"]},
        component: () =>
            import("./layouts/BasicLayout.vue"),
        children: [
            {
                path: "/",
                redirect: "/dashboard/analysis"
            },
            {
                path: "/dashboard",
                name: "dashboard",
                meta: { icon: "dashboard", title: "仪表盘"},
                component: { render: h => h("router-view") },
                children: [
                    {
                        path: "/dashboard/analysis",
                        name: "analysis",
                        meta: {title: "分析页"},
                        component: () =>
                            import("./views/Dashboard/Analysis.vue"),
                    }
                ]
            },
            {
                path: "/form",
                name: "form",
                component: { render: h => h("router-view") },
                meta: { icon: "form", title: "表单" ,authority: ['admin']},
                children: [
                    {
                        path: "/form/basic-form",
                        name: "basicform",
                        meta: {title: "基础表单"},
                        component: () =>
                            import("./views/Forms/BasicForm.vue"),
                    },
                    {
                        path: '/form/step-form',
                        name: 'stepform',
                        hideChildrenInMenu: true,
                        meta: {title: "分布表单"},
                        component: () =>
                            import("./views/Forms/StepForm"),
                        children: [
                            {
                                path: '/form/step-form',
                                redirect: '/form/step-form/info',
                            },
                            {
                                path: '/form/step-form/info',
                                name: 'info',
                                component: () =>
                                    import("./views/Forms/StepForm/Step1.vue"),//
                            },
                            {
                                path: '/form/step-form/confirm',
                                name: 'confirm',
                                component: () =>
                                    import("./views/Forms/StepForm/Step2.vue"),//
                            },
                            {
                                path: '/form/step-form/result',
                                name: 'result',
                                component: () =>
                                    import("./views/Forms/StepForm/Step3.vue"),//
                            },
                        ]

                    }
                ]
            }

        ]

    },
    {
        path: "/403",
        name: "403",
        hideInMenu: true,
        component: Forbidden,
    },
    {
        path: "*",
        name: "404",
        hideInMenu: true,
        component: NotFound,
    },
    {
        path: "/about",
        name: "About",
        hideInMenu: true,
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import(/* webpackChunkName: "about" */ "./views/About.vue"),
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to,from,next) =>{
    if(to.path !== from.path){
        NProgress.start();
    }
    const record = findLast(to.matched, record => record.meta.authority);
    if(record && !check(record.meta.authority)) {
        if(!isLogin && to.path !== '/user/login'){
            next({
                path: '/user/login'
            });
        } else if(to.path !== '/403'){
            notification.error({
                message: '403',
                description: '你没有权限，请联系管理员。'
            });
            next({
                path: '/403'
            });
        }
        NProgress.done();
    }
    next();
})

router.afterEach(() => {
    NProgress.done();
})

export default router;
